package com.wsdk;

import android.app.Application;
import android.util.DisplayMetrics;

import com.wsdk.constants.UrlConstants;
import com.wsdk.exception.ErrorDefination;
import com.wsdk.exception.WsdkException;
import com.wsdk.util.ShowToast;

/**
 * @author fionera
 */
public class Wsdk {
    public static int screenWidth;
    public static float screenDensity;
    public static float scaledDensity;
    private static Application application;

    private static boolean useEmbedLogic;

    public static Application getInstance() {
        return application;
    }

    public static boolean isUseEmbedLogic() {
        return useEmbedLogic;
    }

    private static void init(Application application) throws WsdkException {
        Wsdk.application = application;
        getDisplayMetrics();

        if(!checkHasOkHttp()) {
            ShowToast.shortTime(ErrorDefination.ERR_NO_OKHTTP_MSG);
            throw new WsdkException(ErrorDefination.ERR_NO_OKHTTP, ErrorDefination.ERR_NO_OKHTTP_MSG);
        }
    }

    public static void init(Application application, WsdkConfig wsdkConfig) throws WsdkException {
        Wsdk.init(application);
        UrlConstants.ROOTURL = wsdkConfig.getRootUrl();
        Wsdk.useEmbedLogic = wsdkConfig.isUseEmbedLogic();
    }

    private static void getDisplayMetrics() {
        DisplayMetrics metric = application.getResources().getDisplayMetrics();
        screenWidth = metric.widthPixels;
        screenDensity = metric.density;
        scaledDensity = metric.scaledDensity;
    }

    private static boolean checkHasOkHttp() {
        try {
            Class.forName("okhttp3.OkHttpClient");
            return true;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return false;
    }
}

